﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint.LookupPicker (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Portal.WebControls;
using System.Collections.Generic;
using Res = Sapiens.at.SharePoint.Fields.Resources.Sapiens_at_SharePoint_LookupPicker;
using System.Collections;

namespace Sapiens.at.SharePoint.Fields.WebParts.LookupFilter
{
    [ToolboxItemAttribute(false)]
    public class LookupFilter : UserInputFilterWebPart
    {
        internal LookupPicker Picker { get; set; }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            Controls.Add(new CssRegistration() { Name = "forms.css" });
            Picker = new LookupPicker()
            { 
                AllowEmpty = true, DoPostBackOnResolve = true,
                
                PickerData = new LookupPickerData(
                    WebID,
                    ListID,
                    LookupField,
                    AdditionalSearchFields,
                    AdditionalDisplayFields,
                    "ID",
                    false,
                    AllowShowAll,
                    ContentTypes,
                    SearchScope)
            };
            Picker.Style.Add("margin", "1px");
            Controls.Add(Picker);
        }

        public override EditorPartCollection CreateEditorParts()
        {
            return new EditorPartCollection(base.CreateEditorParts(), new EditorPart[] { new LookupFilterEditorPart { ID="LookupFilterEditorPart", Title = Res.LookupSettings } });
        }

        protected override bool AutoPostBack
        {
            get
            {
                EnsureChildControls();
                return Picker.AutoPostBack;
            }
            set
            {
                EnsureChildControls();
                Picker.AutoPostBack = value;
            }
        }

        protected override string[] GetUserSelectedValues()
        {
            EnsureChildControls();
            if (Picker.ResolvedEntities.Count > 0)
            {
                return new string[]{((PickerEntity)Picker.ResolvedEntities[0]).DisplayText};
            }
            return new string[0];
        }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public string[] AdditionalDisplayFields { get; set; }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public string[] AdditionalSearchFields { get; set; }

        public string[] AllDisplayFields
        {
            get
            {
                var list = new List<string>(AdditionalDisplayFields);
                list.Insert(0, LookupField);
                return list.ToArray();
            }
        }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public bool AllowMultiValue { get; set; }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public bool AllowShowAll { get; set; }

        public string[] AllSearchFields
        {
            get
            {
                var list = new List<string>(AdditionalSearchFields);
                list.Insert(0, LookupField);
                return list.ToArray();
            }
        }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public string[] ContentTypes { get; set; }

        public string IDFieldName { get { return "ID"; } }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public Guid ListID { get; set; }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public string LookupField { get; set; }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public string SearchScope { get; set; }

        [WebBrowsable(false), Personalizable(PersonalizationScope.Shared)]
        public Guid WebID { get; set; }
    }
}
